<?php
include_once '../common.func.php';

$navs = getNavs();
$links = getLinks();
$categorys = getCategorys();
$tags = getTags();
$settings = getSettings();
$relatedPosts = getRelatedPosts($post['url']);

$tagsHtml = '';
foreach ($post['tag'] as $tag) {
    $tagsHtml .= sprintf('<a href="%s" title="查看此标签所有文章">%s</a>&nbsp;', $settings['base'] . '/tag/' . $tag . '.html', $tag);
}
?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title><?php echo $post['title'] ?></title>
    <script src="<?php echo $settings['base'] ?>/themes/default/js/html5.js"></script>
    <link href="<?php echo $settings['base'] ?>/css/normalize.css" rel="stylesheet" />
    <link href="<?php echo $settings['base'] ?>/css/article.css" rel="stylesheet" />
</head>
<body>
<header>
    <h1><span><?php echo $settings['title'] ?></span></h1>

    <p><?php echo $settings['describe'] ?></p>
    <nav>
        <ul>
            <?php foreach ($navs as $nav) { ?>
            <li><a href="<?php echo $nav['href'] ?>" target="<?php echo $nav['target'] ?>"><?php echo $nav['text'] ?>&nbsp;</a></li>
            <?php } ?>
        </ul>
    </nav>
</header>
<div id="content-wrapper">
    <aside>
        <nav>
            <h1>分类</h1>
            <ul>
                <?php foreach ($categorys as $id => $category) { ?>
                <li><a href="<?php echo $settings['base'] . '/category/' . $category['name'] . '.html' ?>" title="查看 <?php echo $category['name'] ?> 的全部文章"><?php echo $category['name'] ?></a></li>
                <?php } ?>
            </ul>
        </nav>
        <nav class="tag-nav">
            <h1>标签</h1>
            <ul>
                <?php foreach ($tags as $li) { ?>
                <li><a href="<?php echo $settings['base'] . '/tag/' . $li['name'] . '.html' ?>"><?php echo $li['name'] ?></a></li>
                <?php } ?>
            </ul>
        </nav>
        <nav>
            <h1>链接</h1>
            <ul>
                <?php foreach ($links as $link) { ?>
                <li><a href="<?php echo $link['href'] ?>" target="_blank" title="<?php echo $link['title'] ?>"><?php echo $link['content'] ?></a></li>
                <?php } ?>
            </ul>
        </nav>
    </aside>
    <section>
        <article class="post blog-content">
            <header class="post-header">
                <h2><?php echo $post['title'] ?></h2>

                <p>
                <?php echo date('Y-m-d H:i:s', intval($post['time'])); ?> | 
                <?php echo AUTHOR ?> | 
                <a href="<?php echo $settings['base'] ?>/post/<?php echo $post['url'] ?>.html#comments" title="查看评论"><?php echo count($post['comments']) ?> Comments</a> | 
                <?php 
                $categoryName = getCategoryNameById($post['category']);
                echo sprintf('<a href="%s/category/%s.html" title="查看 %s 的全部文章">%s</a>', $settings['base'], $categoryName, $categoryName, $categoryName);

                if (count($post['tag']) == 0) { 
                } else {
                    echo ' | ';
                    foreach ($post['tag'] as $postTag) {
                        echo sprintf('<a href="%s/tag/%s.html" title="所属标签 %s">%s</a>&nbsp;', $settings['base'], $postTag, $postTag, $postTag);
                    }
                }
                ?></p>
            </header>
            <div>
            <?php echo $post['content']; ?>
            </div>
            <footer class="post-footer">
                <div class="author-wrapper">
                    <p>Written by ishow</p>
                </div>
                <nav class="related-wrapper">
                    <h3>Related Posts</h3>
                    <?php 
                    if (count($relatedPosts) == 0) {
                        echo '<p>暂无相关文章</p>';
                    } else {
                        echo '<ul>';
                        foreach ($relatedPosts as $relatedPostId) {
                            $relatedPost = getPostById($relatedPostId);
                            echo sprintf('<li><a href="%s/post/%s.html">%s</a></li>', $settings['base'], $relatedPostId, $relatedPost['title']);
                        }
                        echo '</ul>';
                    }
                    ?>
                </nav>
            </footer>
        </article>
        <section id="comments-wrapper">
            <div id="comments">
                <h2>评论</h2>
                <?php
                if (count($post['comments']) == 0) {
                    echo '<article><p>暂无评论</p></article>';
                }
                foreach ($post['comments'] as $comment) {
                ?>
                <article>
                    <footer>
                        <?php
                        if ($comment['website']) {
                        ?>
                        <p>
                            <a href="<?php echo $comment['website'] ?>" target="_blank" title="<?php echo $comment['website'] ?>"><?php echo $comment['name'] ?></a>
                        </p>
                        <?php
                        } else {
                        ?>
                        <p><?php echo $comment['name'] ?></p>
                        <?php } ?>

                        <p>
                            <time datetime="<?php echo $comment['time'] ?>"><?php echo $comment['time'] ?></time>
                        </p>
                    </footer>
                    <p><?php echo htmlspecialchars($comment['content']) ?></p>
                </article>
                <?php
                }
                ?>
            </div>
            <div id="reply">
                <h2>发表评论：</h2>

                <form action="<?php echo $settings['base'] ?>/admin/action/reply.action.php" method="POST">
                    <fieldset class="required">
                        <div>
                            <label for="name">昵称</label>
                            <input type="text" name="name" id="name" required="required" />
                        </div>
                        <div>
                            <label for="mail">邮件地址</label>
                            <input type="text" name="mail" id="mail" />
                        </div>
                        <div>
                            <label for="website">个人主页</label>
                            <input type="text" name="website" id="website" />
                        </div>
                    </fieldset>
                    <fieldset class="required">
                        <div>
                            <label for="content">内容</label>
                            <textarea id="content" name="content" required="required"></textarea>
                        </div>
                    </fieldset>
                    <fieldset>
                        <div>
                            <input type="hidden" name="key" value="<?php echo $post['url'] ?>" />
                            <input type="submit" value="发表评论" />
                        </div>
                    </fieldset>
                </form>
            </div>
        </section>
    </section>
</div>
<footer>
    <p>Powered by Paper Blog</p>
</footer>
</body>
</html>
